<template>
  <div class="commodity-information">
    <el-card
      class="search_card h5Card"
      ref="searchCardRef">
      <el-form
        :model="formData"
        class="search_form">
        <el-row>
          <el-form-item label="米厂简称：">
            <el-select
              v-model="formData.baseRiceBarnId"
              v-loading="riceFactoryLoading"
              placeholder="米厂简称"
              filterable
              clearable>
              <el-option
                v-for="item in riceFactorySimpleNameList"
                :key="item.id"
                :label="item.simple_name"
                :value="item.id" />
            </el-select>
          </el-form-item>
          <div
            v-if="isExpand"
            style="width: 100%;margin-right: 10px">
            <el-form-item label="启用状态：">
              <el-select
                v-model="formData.isEnable"
                placeholder="请选择"
                style="width: 100%"
                clearable>
                <el-option
                  :label="config.button.text.enable"
                  :value="true" />
                <el-option
                  :label="config.button.text.stop"
                  :value="false" />
              </el-select>
            </el-form-item>
          </div>
        </el-row>
      </el-form>
      <div class="top-btn">
        <div
          class="expanded"
          @click="isExpand=!isExpand">
          <span v-if="!isExpand">
            <span>展开</span>
            <el-icon><ArrowDown /></el-icon>
          </span>
          <span v-else>
            <span>收起</span>
            <el-icon><ArrowUp /></el-icon>
          </span>
        </div>
        <div>
          <el-button
            type="primary"
            @click="getList(formData)"
            class="search">
            <template #icon>
              <el-icon>
                <search />
              </el-icon>
            </template>
            {{ config.button.text.query }}
          </el-button>
          <el-button @click="reset">
            <template #icon>
              <el-icon>
                <Refresh />
              </el-icon>
            </template>
            {{ config.button.text.reset }}
          </el-button>
          <el-button
            v-permission="validateButtonPermission(route, 0)"
            @click="addEvent"
            type="info">
            <template #icon>
              <el-icon>
                <CirclePlus />
              </el-icon>
            </template>
            {{ config.button.text.add }}
          </el-button>
        </div>
      </div>
    </el-card>
    <div :style="{ height: tableHeight,overflow: 'auto'}">
      <el-card
        class="table_card info-card"
        v-if="tableData.length>0"
        v-for="item in tableData"
        :key="item.id"
        @click="editEvent(item)">
        <div class="info-item">
          <div class="info-title">米厂简称：</div>
          <div class="info-main">{{ item.baseRiceBarnSimpleName }}</div>
        </div>
        <div class="info-item">
          <div class="info-title">商品全称：</div>
          <div class="info-main">{{ item.fullName }}</div>
        </div>
        <div class="info-item">
          <div class="info-title">商品简称：</div>
          <div class="info-main">{{ item.simpleName }}</div>
        </div>
        <div class="info-item">
          <div class="info-title">商品品种：</div>
          <div class="info-main">{{ item.variety }}</div>
        </div>
        <div class="info-item">
          <div class="info-title">袋皮全称：</div>
          <div class="info-main">{{ item.bagInfoFullName }}</div>
        </div>
        <div class="info-item">
          <div class="info-title">商品地区：</div>
          <div class="info-main">{{ item.region }}</div>
        </div>
        <div class="info-item">
          <div class="info-title">启用状态：</div>
          <div class="info-main">
            <el-tag :type="item.isEnable ? 'primary' : 'danger'">
              {{ item.isEnable ? config.button.text.enable
                : config.button.text.stop }}
            </el-tag>
          </div>
        </div>
      </el-card>
      <noDataPage v-else/>
    </div>
    <!-- 查看 -->
    <View
      ref="viewRef"
      :rowData="rowData"
      @edit-info="editEvent" />
    <!-- 新增修改 -->
    <Edit
      ref="editRef"
      :rowData="rowData"
      @save="save" />
  </div>
</template>

<script setup>
import {ref, onMounted, watch} from "vue";
import ApiBaseRiceBarnGoods from "@/api/modules/base-rice-barn-goods"
import { useTable } from "@/hooks/useTable"
import View from "./component/View.vue"
import Edit from "./component/Edit.vue"
import message from "@/common/element-plus/message";
import config from "@/common/element-plus/config";
import app from "@/common/config/app";
import { validateButtonPermission } from "@/common/utils/validate";
import { useRoute } from "vue-router"
import { useAppStore } from "@/stores/app"
import { storeToRefs } from "pinia";
import noDataPage from "@/components/noDataPage.vue";

const route = useRoute()
// 查询表单
const formData = ref({})
// 查看组件实例
const viewRef = ref()
// 新增修改组件实例
const editRef = ref()
// 表格每行数据
const rowData = ref({})
// 表格高度(固定表头)
const height = ref(0)
// 查询卡片实例
const searchCardRef = ref()

//收缩展开
const isExpand = ref(false);

const tableHeight = ref("calc(100vh - 250px)")

const riceFactorySimpleNameList = ref([])

const { tableMethods, tableState, pagination } = useTable({
  fetchDataApi: async (data) => {
    const { pageSize, pageNum } = pagination
    const res = await ApiBaseRiceBarnGoods.selectPage(data, {
      pageNum: pageNum.value,
      pageSize: pageSize.value,
    })
    return {
      list: res.data.records,
      total: res.data.total
    }
  }
})
const { getList } = tableMethods
const { tableData } = tableState

// 重置
const reset = () => {
  formData.value = {}
  getList()
}

//获取米厂简称列表
const getRiceFactorySimpleNameList = async () => {
  const data = await ApiBaseRiceBarnGoods.getSimpleNameList("/baseRiceBarn/")
  riceFactorySimpleNameList.value = data.data
}

// 打开查看对话框
const viewEvent = (row) => {
  viewRef.value.visibility = true
  rowData.value = row
}

// 打开新增对话框
const addEvent = () => {
  editRef.value.visibility = true
  rowData.value = {}
}

// 打开修改对话框
const editEvent = (row) => {
  editRef.value.visibility = true
  rowData.value = Object.assign({}, row)
}

// 保存后回调
const save = async () => {
  editRef.value.visibility = false
  message.success()
  await getList()
}

// 监听查询卡片大小变化
const resizeObserver = new ResizeObserver(entries => {
  for (const entry of entries) {
    if (contentFullScreen.value) {
      height.value = (window.innerHeight - app.otherHeight - app.tagHeight - entry.contentRect.height) + "px"
    } else {
      height.value = (window.innerHeight - app.otherHeight - app.tagHeight - app.headerHeight - entry.contentRect.height) + "px"
    }
  }
})

//监听是否展开动态获取下面的高度
watch(() => isExpand.value, (newVal) => {
  if (newVal&&isExpand.value) {
    setTimeout(() => {
      let searchCards = document.querySelector(".h5Card");
      tableHeight.value = `calc(100vh - 95px - ${searchCards.offsetHeight}px)`
    },200);
  } else {
    tableHeight.value = "calc(100vh - 205px)"
  }
}, { immediate: true })

const appStore = useAppStore()

// 保持响应式
const { contentFullScreen } = storeToRefs(appStore)

onMounted(() => {
  resizeObserver.observe(searchCardRef.value.$el)
  getRiceFactorySimpleNameList()
})
</script>

<style lang="scss" scoped>
.commodity-information {
  .search_card {
    margin-bottom: 4px;

    :deep(.el-card__body) {
      padding: 10px;
      // display: flex;
      justify-content: space-between;
    }

    .el-button {
      margin: 5px 10px 5px 0;
    }

    .el-form-item {
      margin-right: 10px;
      padding: 5px 0;
    }

    .top-btn{
      margin-top: 5px;
      display: flex;
      align-items: center;
      justify-content: space-between;
    }
  }

  .expanded{
    margin-right: 10px;
    text-align: right;
    color:#e6a23c;

    span{
      font-size: 14px;
    }
  }

  .table_card {
    :deep(.el-card__body) {
      padding: 0;
    }
  }

  .info-card {
    padding: 10px;
    margin-bottom: 3px;
    .info-item {
      display: flex;
      padding: 5px 0;
      .info-title{
        width: 100px;
        color: #999;
      }
      .info-main{
        flex: 1;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
    }
  }
}

.top-btn{
  text-align: right;
  color: #409eff;

  .top-btn-item{
    // display: flex;
    // align-items: center;
    margin-right: 5px;
  }
}

:deep(.el-form-item__label){
  width: 110px;
}

.el-form-item{
  width: 100%;
}
</style>
  